package com.woshua.vptabmodel.transformer;

import android.support.annotation.NonNull;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.woshua.vptabmodel.utils.L;

/**
 * @author Steven.zhao
 * email:hongtu.zhao@goodwinsoft.net
 * date:2019/7/20
 * desc:
 */
public class ScaleTransformer implements ViewPager.PageTransformer {

    private static final float MIN_SCALE=0.85f;
    private static final float MIN_ALPHA=0.5f;

    @Override
    public void transformPage(@NonNull View view, float progress) {
//        L.d("tag:"+view+"----progress:"+progress);

        //a->b
//        a:pos:(0,-1)
//        b:pos(1,0)
//        b->a
//        a:pos:(-1,0)
//        b:pos(0,1)
//        [,-1)
          if(progress<-1)
          {
              view.setScaleX(MIN_SCALE);
              view.setScaleY(MIN_SCALE);
              view.setAlpha(MIN_ALPHA);
          }
//          [-1,1]
          else if(progress<=1)
          {
//              左边的页面
              if(progress<0)
              {
                  //a:[1,0.75] (0,-1)
                  float scaleA=MIN_SCALE+(1-MIN_SCALE)*(1+progress);
                  view.setScaleX(scaleA);
                  view.setScaleY(scaleA);

                  //a [1,0.5]
                  float alphaA=MIN_ALPHA+(1-MIN_ALPHA)*(1+progress);
                  view.setAlpha(alphaA);

//                  b:[0.75,1]
              }
              else
              {
//                  右边的页面
                  //        b:pos(1,0)
                  float scaleB=MIN_SCALE+(1-MIN_SCALE)*(1-progress);
                  view.setScaleX(scaleB);
                  view.setScaleY(scaleB);

                  //a [0.5 1]
                  float alphaB=MIN_ALPHA+(1-MIN_ALPHA)*(1-progress);
                  view.setAlpha(alphaB);
              }
          }
//          (1,]
          else
          {
              view.setScaleX(MIN_SCALE);
              view.setScaleY(MIN_SCALE);

              view.setAlpha(MIN_ALPHA);
          }
    }
}
